/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Iterator;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import service.Planet;
import service.PlanetFacadeREST;
import service.Tag;
import service.TagFacadeREST;

/**
 *
 * @author andrew
 */
public class insertTagServlet extends HttpServlet {

    @EJB
    private PlanetFacadeREST planetManager;
    
    @EJB
    private TagFacadeREST tagManager;
    
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        // check if session is expired
        HttpSession session = request.getSession();// don't create if it doesn't exist
//        if(session != null && !session.isNew()) {} 
//        else {
//            response.sendRedirect("/home.jsp");
//        }
        
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        
        String editingPlanetIdString = request.getParameter("editingPlanetId");
        String tagName = request.getParameter("tagName");
        
        int editingPlanetId = Integer.parseInt(editingPlanetIdString);
        
        Planet planet = planetManager.find(editingPlanetId);
        
        Collection<Tag> collTags = planet.getTagCollection();
        
        Tag t = new Tag();
        
       // t.setPlanetidPlanet(planet);
        
        t.setDescription(tagName);
        
        collTags.add(t);
        
        planet.setTagCollection(collTags);
        
        planetManager.edit(planet);
        
        
        collTags = tagManager.findAll();
        
        int newIdTag = -1;
        
        Iterator iterator = collTags.iterator();
        while (iterator.hasNext()) {     
             Tag tag = (Tag) iterator.next();
             if(tag.getDescription().equals(tagName)){
                 Collection<Planet> collPlanet = tag.getPlanetCollection();
                 collPlanet.add(planet);
                 tag.setPlanetCollection(collPlanet);
                 tagManager.edit(tag);
                 newIdTag = tag.getIdTag();
             }
        } 
        
        
        
        out.print(newIdTag);
        out.close();
        
        
        
        
        
        
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
